﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Utils;
using System.Data.SqlClient;
using Settings;

namespace SMSROUTER.Business
{
    public class SmsOutReportStruct
    {
        public int? RouterID { get; set; }
        public Guid? SmsOutGUID { get; set; }
        public string CooperateGUID { get; set; }
        public DateTime? SentTime { get; set; }
        public DateTime? ReportTime { get; set; }
        public int? RetryOnFailReqTimes { get; set; }
        public int? RetryOnTimeOutReqTimes { get; set; }
        public string Status { get; set; }
        public int? ThreadID { get; set; }

        public SmsOutReportStruct() { } // *Bắt buộc phải có hàm tạo ko đối số thì xml mới serialize được.

        public SmsOutReportStruct(int? routerID, Guid? smsOutGUID, string cooperateGUID, DateTime? sentTime, DateTime? reportTime, int? retryOnFailReqTimes, int? retryOnTimeOutReqTimes, string status, int? threadId)
        {
            this.RouterID = routerID;
            this.SmsOutGUID = smsOutGUID;
            this.CooperateGUID = cooperateGUID;
            this.SentTime = sentTime;
            this.ReportTime = reportTime;
            this.RetryOnFailReqTimes = retryOnFailReqTimes;
            this.RetryOnTimeOutReqTimes = retryOnTimeOutReqTimes;
            this.Status = status;
            this.ThreadID = threadId;
        }

        public static IList<SmsOutReportStruct> CreateList(IList<SmsOutWaiting> smsList, DateTime sentTime, DateTime? reportTime, string status, int? threadId)
        {
            IList<SmsOutReportStruct> retList = new List<SmsOutReportStruct>();
            if (smsList == null)
                return retList;
            foreach (var item in smsList)
            {
                retList.Add(new SmsOutReportStruct(item.RouterID, item.SmsOutGUID, item.CooperateGUID, sentTime, reportTime, item.RetryOnFailReqTimes, item.RetryOnTimeOutReqTimes, status, threadId));
            }
            return retList;
        }

        public static void BatchUpdate(IList<SmsOutReportStruct> list)
        {
            if (list.Count > 0)
            {
                try
                {
                    DataAccess.BatchExecute<SmsOutReportStruct>(list, "@SmsOutReport", "SmsOutReportStructType", "usp_SmsOutReport_Update", "RouterID");
                }
                catch (Exception ex)
                {
                    Log.InsertLog(ex, null, AppSetting.WarningType.mt_error, null);
                    try
                    {
                        Xml.WriteListToFile<SmsOutReportStruct>(list, AppSetting.SmsOutReportBackupFileName);
                    }
                    catch (Exception ex1)
                    {
                        Log.InsertLog("SmsOutReport Write to Xml has fail|" + ex1.Message, ex1.StackTrace, ex1.Source, AppSetting.WarningType.mt_error, null);
                    }
                }
            }
        }

        public static void DeleteReport(SqlTransaction trans, string status)
        {
            SqlHelper.ExecuteNonQuery(trans, CommandType.StoredProcedure, "usp_SmsOutReport_Delete", new SqlParameter("@BatchSize", AppSetting.BatchSize), new SqlParameter("@Status", status));
        }

        public static void DeleteReport(string status)
        {
            using (SqlConnection conn = AppSetting.GetConnection())
            {
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    DeleteReport(trans, status);
                    trans.Commit();
                }
            }
        }
    }
}
